import React, { Component } from 'react'
import { connect } from 'dva';
import { Switch,Route,Redirect,withRouter } from 'dva/router';
import dynamic from 'dva/dynamic'
import { newMenuTransformationRouters } from './utils/dataConversion'
import Personal from './pages/modulars/personal/Index';
import NotFound from './pages/common/404';
class BeforeEach extends Component {
    UNSAFE_componentWillMount(){
        const { userinfo, dispatch } = this.props
        const menuList = userinfo.menuList
        const data = newMenuTransformationRouters(menuList)
        dispatch({
            type: 'userinfo/updateRouterPath',
            action: data
        })
    }
    renderRoutesMap = data => {
        return data.map(item =><Route key={item.path} exact path={item.path} component={dynamic({
            component: () => import(`${item.component}`)
        })}></Route>)
    }
    render() {
        const { routerItems } = this.props.userinfo
        const { pathname } = this.props.location
        return (
            <Switch>
                {
                  pathname === "/" ? <Redirect to="dashboard" />:''
                }
                {
                  this.renderRoutesMap(routerItems)
                }
                <Route key="/personal" exact path="/personal" component={Personal} ></Route>
                <Route path="*" component={NotFound}></Route>
              </Switch>
        )
    }
}
const mapStateToProps = state => {
    return {
        userinfo: state.userinfo
    }
}
export default withRouter(connect(mapStateToProps)(BeforeEach));